package com.hsmw.api.controller.biz;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.service.HsmwConstructionUnitService;
import com.hsmw.api.service.HsmwDriverOrderRecordService;
import com.hsmw.domain.*;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.htn.common.core.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "BIZ-公司定制化服务系统")
@RestController
@RequestMapping("/api/new/biz")
public class NewBizController {


    @Autowired
    private HsmwDriverOrderRecordService hsmwDriverOrderRecordService;

    @Autowired
    private CurrentHsmwUserUtils currentHsmwUserUtils;

    @Autowired
    private HsmwConstructionUnitService hsmwConstructionUnitService;



    @ApiOperation(value = "新首页接口", notes = "首页接口", produces = "application/json")
    @PostMapping("/getNewHomeData")
    public DataResponse getNewHomeData(){

        Map map = new HashMap();
        HsmwBizUser userInfo = currentHsmwUserUtils.getCurrentBizUserInfo();
        if(userInfo == null){
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"获取当前登陆人信息错误");
        }
        String ownCompanyId = userInfo.getOwnCompanyId();
        // 根据公司id记录该公司所有项目已完成的订单数量
        int orderCount = hsmwDriverOrderRecordService.queryOrderCount(ownCompanyId);
        map.put("orderCOunt",orderCount);

        //记录该公司所有完成订单的实际方量，保留小叔点后两位
        Double realVolume = hsmwDriverOrderRecordService.queryRealVolumeByCompanyId(ownCompanyId);
        map.put("realVolume",realVolume);

        return new DataResponse(map);
    }


    @ApiOperation(value = "新增公司", notes = "新增公司", produces = "application/json")
    @PostMapping("/addCompany")
    public DataResponse addCompany(@RequestBody HsmwConstructionUnit hsmwConstructionUnit){

        if(hsmwConstructionUnit == null){
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"传递参数错误");
        }

        if(StringUtils.isNotBlank(hsmwConstructionUnit.getName())){
            int count = hsmwConstructionUnitService.count(new LambdaQueryWrapper<HsmwConstructionUnit>()
                    .eq(HsmwConstructionUnit::getName, hsmwConstructionUnit.getName())
                    .eq(HsmwConstructionUnit::getCreateUserCompanyId, hsmwConstructionUnit.getCreateUserCompanyId())
            );
            if(count > 0){
                return new DataResponse(CommonConstants.ResponseStatus.FAIL,"公司名称不可重复");
            }
        }

        return hsmwConstructionUnitService.addCompany(hsmwConstructionUnit);
    }


    @ApiOperation(value = "查询当前登陆人公司列表信息", notes = "查询当前登陆人公司列表信息", produces = "application/json")
    @ApiImplicitParam(name = "id", value = "当前登陆人的公司id",dataType = "String",required = true)
    @GetMapping("/queryCompanyList")
    public DataResponse queryCompanyList(String id){

        if(StringUtils.isBlank(id)){
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"传递参数错误，请检查！");
        }
        List<HsmwConstructionUnit> list = hsmwConstructionUnitService.list(new LambdaQueryWrapper<HsmwConstructionUnit>().eq(HsmwConstructionUnit::getCreateUserCompanyId, id));

        return new DataResponse(CommonConstants.ResponseStatus.SUCCESS,list);

    }


}
